import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—08
 * Time: 14:31
 */
class Demo03 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if(!isOperation(x)) {
                //不是加减乘除
                //字符转成整数.放进栈中
                stack.push(Integer.parseInt(x));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(x) {
                    case "+": stack.push(num1 + num2);
                        break;
                    case "-": stack.push(num1 - num2);
                        break;
                    case "*": stack.push(num1 * num2);
                        break;
                    case "/": stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperation(String opera) {
        if(opera.equals('+') || opera.equals('-') || opera.equals('*') || opera.equals('/')) {
            return true;
        }
        return false;
    }

}